package com.zk.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.mongodb.client.result.UpdateResult;
import com.zk.entity.MongoDBOrder;
import com.zk.service.IMongoDBService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;


import static com.mongodb.client.model.Filters.eq;


@Service
public class MongoDBServiceImpl implements IMongoDBService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public boolean addOrder(MongoDBOrder a){
//        System.out.println(a.toString());
//        System.out.println(a.getDetail().toString());
        try {
            MongoDBOrder result = mongoTemplate.save(a,"order");
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean updateOrder(String id ,JSONObject jsonObject) {
        try {
            Query query = new Query(Criteria.where("_id").is(id));
            Update update = new Update();
            jsonObject.forEach((key,value) ->{
                    if (!key.equals("_id")){
                        update.set(key,value);
                    }
            });
            mongoTemplate.updateFirst(query,update,"order");
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean deleteOrder(String id) {
        try{
            Query query = Query.query(Criteria.where("_id").is(new ObjectId(id)));
            Update update = new Update();
            update.set("isDel",1);
            UpdateResult result = mongoTemplate.updateFirst(query,update,"order");
            System.out.println(result.getModifiedCount());
            if (result.getModifiedCount() == 1)
                return true;
            else
                return false;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

//    @Override
//    public String findOrderById(String id) {
//        try {
//            MongoCollection<Document> collection = mongoTemplate.getCollection("order");
//
//            Bson bson = eq("_id",new ObjectId(id));
//            FindIterable<Document> documents = collection.find(bson);
//            List list = new ArrayList();
//            for(Document document : documents) {
//                list.add(document.toJson());
//                System.out.println(document.toJson());
//            }
//
//            return list.toString();
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//    @Override
//    public String findOrderByUser(String username) {
//
//        try {
//            MongoCollection<Document> collection = mongoTemplate.getCollection("order");
//            Bson bson = eq("username",username);
//            FindIterable<Document> documents = collection.find(bson);
//            List list = new ArrayList();
//            for(Document document : documents) {
//                list.add(document.toJson());
//            }
//            return list.toString();
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return null;
//    }
    @Override
    public MongoDBOrder findOrderById(String ida) {
        try {
            Query query = Query.query(Criteria.where("_id").is(ida));
            query.addCriteria(Criteria.where("isDel").is(0));
            return mongoTemplate.findOne(query, MongoDBOrder.class);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<MongoDBOrder> findOrderByUser(String username) {

        try {
            Query query = Query.query(Criteria.where("user_name").is(username));
            query.addCriteria(Criteria.where("isDel").is(0));
            return mongoTemplate.find(query, MongoDBOrder.class);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
